<template>
  <div class="login_con">
    <h2 class="titleh1">{{$t('reg[6]')}}</h2>
    <section class="frommain" v-if="single">
      <div class="fromcont">
        <el-form
          label-position="top"
          label-width="80px"
          :rules="rules"
          ref="ruleForm"
          :model="formLabelAlign"
        >
          <el-form-item :label="$t('mobile[1]')" prop="email">
            <el-input v-model="formLabelAlign.email" :placeholder="$t('mobile[2]')"></el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" size="medium" @click="nextstep">{{$t('mobile[3]')}}</el-button>
      </div>
      <div class="resetpassword">
        <router-link to="phonepassword">{{$t('mobile[12]')}}</router-link>
      </div>
    </section>

    <section class="frommain" v-else>
      <div class="fromcont">
        <el-form
          label-position="top"
          label-width="80px"
          :rules="rules1"
          ref="ruleForm1"
          :model="formLabelAlign"
        >
          <el-form-item :label="$t('mobile[4]')" prop="password">
            <el-input
              v-model="formLabelAlign.password"
              :placeholder="$t('mobile[5]')"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item :label="$t('mobile[6]')" prop="password_confirmation">
            <el-input
              v-model="formLabelAlign.password_confirmation"
              :placeholder="$t('reg[5]')"
              show-password
            ></el-input>
          </el-form-item>
          <!-- <div class="resetfooter">
                        <p>{{$t('mobile[7]')}} {{Displaycharacter}} {{$t('mobile[8]')}}</p>
          </div>-->
          <el-form-item :label="$t('mobile[9]')" prop="code">
            <el-input v-model="formLabelAlign.v_code" :placeholder="$t('mobile[10]')">
              <template slot="append">
                <el-button @click="Verificationfun()">{{Verification.text}}</el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
        <el-button type="primary" size="medium" @click="handleSubmit()">{{$t('mobile[11]')}}</el-button>
        <div class="resetpassword">
          <router-link to="phonepassword">{{$t('mobile[12]')}}</router-link>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import { forgetPassword } from "@/api/user";
// import { mapActions } from 'vuex'
import { Hidephone, EmailSms, Passwordencryption } from "@/libs/util";
export default {
  name: "login",
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("mobile[13]")));
      } else if (EmailSms(value)) {
        if (this.$public.email(value)) {
          callback();
        } else {
          callback(new Error(this.$t("reg[10]")));
        }
      } else {
        if (this.$public.phone(value)) {
          callback();
        } else {
          callback(new Error(this.$t("reg[11]")));
        }
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("reg[12]")));
      } else if (this.$public.pwds(value)) {
        callback();
      } else {
        callback(new Error(this.$t("reg[13]")));
      }
    };
    var validatePass3 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("reg[12]")));
      } else if (value !== this.formLabelAlign.password) {
        callback(new Error(this.$t("mobile[14]")));
      } else {
        callback();
      }
    };
    return {
      formLabelAlign: {
        email: "",
        password: "",
        password_confirmation: "",
        v_code: ""
      },
      single: true,
      Displaycharacter: "",
      Verification: {
        text: this.$t("reg[0]"),
        time: 60,
        type: true,
        timer: ""
      },
      rules: {
        email: [{ validator: validatePass1, trigger: "blur" }]
      },
      rules1: {
        password: [{ validator: validatePass2, trigger: "blur" }],
        password_confirmation: [{ validator: validatePass3, trigger: "blur" }],
        code: [
          // { required: true, message: this.$t("mobile[15]"), trigger: "change" }
        ]
      }
    };
  },
  methods: {
    handleSubmit() {
      this.$refs["ruleForm1"].validate(valid => {
        if (valid) {
          let data = this.formLabelAlign;
          forgetPassword(Passwordencryption(this.formLabelAlign))
            .then(res => {
            
                this.$message({
                  message: this.$t("mobile[16]"),
                  type: "success"
                });
                setTimeout(() => {
                  this.$router.push({ path: "/login" });
                }, 800);
             
            })
            .catch(err => {
              this.$message.error(err.msg);
            });
        } else {
          return false;
        }
      });
    },
    nextstep() {
      //下一步
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.single = false;
          this.Displaycharacter = Hidephone(this.formLabelAlign.email);
        } else {
          return false;
        }
      });
    },
    Verificationfun() {
      if (!this.Verification.type) {
        return false;
      }
      var _data = {
        type: "2",
        phone: this.formLabelAlign.email,
      };
      this.$public.captcha(_data, this);
    },
    typecaptcha() {
      this.Verification.type = false;
      this.Verification.timer = setInterval(() => {
        this.Verification.time--;
        if (this.Verification.time < 1) {
          clearInterval(this.Verification.timer);
          this.Verification.type = true;
          this.Verification.time = 60;
          this.Verification.text = this.$t("reg[1]");
        } else {
          this.Verification.text = this.Verification.time + "S";
        }
      }, 1000);
    }
  },
  beforeEach: function(to, from, next) {
    clearInterval(this.Verification.timer);
    next();
  }
};
</script>

